package com.runen.wnhz.runen.ui.adapter.puadapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

import com.runen.wnhz.runen.ui.fragment.minem.AllOrderFragment;

import java.util.HashMap;

public class OrderFragmentAdapter extends FragmentPagerAdapter {

    private int fragmentNum;//ViewPager包含的Fragment的数量

    private HashMap<Integer,Fragment> fragmentHashMap = new HashMap<>();//存放键值对数据，key为Fragment在ViewPager的下标，value是Fragment对象

    public OrderFragmentAdapter(FragmentManager fm,int fragmentNum) {
        super(fm);
        this.fragmentNum = fragmentNum;
    }

    @Override
    public Fragment getItem(int position) {
        return createFragment(position);
    }

    @Override
    public int getCount() {
        return fragmentNum;
    }

    /**
     * 创建一个Fragment的对象
     * @param position viewPager被选中的item的下标
     * @return 创建的Fragment对象
     */
    private Fragment createFragment(int position) {
        Fragment fragment = fragmentHashMap.get(position);
        Bundle bundle = new Bundle();
        if (fragment == null) {
            switch (position) {
                case 0:
                    fragment = new AllOrderFragment();
                    bundle.putInt("orderType",-1);//全部订单
                    break;
                case 1:
                    fragment = new AllOrderFragment();
                    bundle.putInt("orderType",1);//待付款订单
                    break;
                case 2:
                    fragment = new AllOrderFragment();
                    bundle.putInt("orderType",2);//待发货订单
                    break;
                case 3:
                    fragment = new AllOrderFragment();
                    bundle.putInt("orderType",3);//待收货订单
                    break;
                case 4:
                    fragment = new AllOrderFragment();
                    bundle.putInt("orderType",4);//已完成订单
                    break;
                case 5:
                    fragment = new AllOrderFragment();
                    bundle.putInt("orderType",0);//已取消订单
                    break;
            }
            fragment.setArguments(bundle);
            fragmentHashMap.put(position, fragment);
        }
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        super.destroyItem(container, position, object);
    }
}
